/**
This file is part of dbdjasperreport.

Copyright (C) 2008 Kue Vang aka Kou
Licensed under the Eclipse Public License, Version 1.0; 
you may not use this file except in compliance with the License. 
You may obtain a copy of the License at http://www.eclipse.org/legal/epl-v10.html 
Unless required by applicable law or agreed to in writing, software 
distributed under the License is distributed on an "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
See the License for the specific language governing permissions and limitations 
under the License.
**/

import java.io.Serializable

class OperatorService implements Serializable {
	List operatorList = new ArrayList()
	List dateOperatorList = new ArrayList()
	List numOperatorList = new ArrayList()
	List strOperatorList = new ArrayList()
	List codeOperatorList = new ArrayList()
	List noneOperatorList = new ArrayList()
	List entityOperatorList = new ArrayList()
	List booleanOperatorList = new ArrayList()
	List yesNoOperatorList = new ArrayList ()
	
	static final String allDates ="1"
	static final String lastWeek = "2"
	static final String lastMonth = "3"
	static final String dateBetween = "4"
	static final String dateGreaterEqual = "5"
	static final String dateLessEqual  = "6"
	static final String dateEqual = "7"
	static final String datePastDays = "8"
	static final String datePastMonths = "9"
	static final String numEqual = "10"
	static final String numGreaterEqual = "11"
	static final String numLessEqual = "12"
	static final String numBetween = "13"
	static final String numNotEqual = "14"
	static final String strStartsWith = "15"
	static final String strContains = "16"
	static final String strMatchesExactly = "17"
	static final String codeIdIsNumber = "18"
	static final String codeIdIsString = "19"
	static final String booleanNone = "20"
	static final String booleanTrue = "21"
	static final String booleanFalse = "22"
	static final String none = "23"
	static final String entityIdIsNumber = "24"
	static final String yesNoNone = "25"
	static final String yesNoY = "26"
	static final String yesNoN = "27"
	
	String getOperatorDesc(String operatorId){
		loadAllOperator()
		
		//first loop gets the array list of Date, Number, String, etc from operatorList because
		//they're stored in an array in operatorList	
		for(int i=0;i<=operatorList.size()-1;i++){
			def operator = operatorList[i]			
			//second loop convert the value in operator to Code
			for(int x=0;x<=operator.size()-1;x++){
				Code code = operator[x]
				//println "ABC strId = " + strId + " code.id " +code.id
				if(operatorId == code.id)
					return code.codeDesc
			}			
		}	
		
	}
	
	String getOperatorDesc(Integer operatorId){
		String strId = operatorId.toString()
		getOperatorDesc(strId)		
	}
		
	List getOperatorList(String dataType){
		//get the operator list for the data type that's passed in
		def dataTypeService = new DataTypeService()
		if(dataType == dataTypeService.dateDataType)
			return loadDateOperator()
		else if(dataType == dataTypeService.numDataType)
			return loadNumberOperator()
		else if(dataType == dataTypeService.strDataType)
			return loadStringOperator()
		else if(dataType == dataTypeService.codeDataType)
			return loadCodeOperator()
		else if(dataType == dataTypeService.imageDataType | 
		        dataType == dataTypeService.rtfDataType)
			return loadNoneOperator()
		else if(dataType == dataTypeService.entityDataType)
			return loadNoneOperator()
		else if(dataType == dataTypeService.booleanDataType)
			return loadBooleanOperator()
		else if(dataType == dataTypeService.yesNoDataType)
			return loadYesNoOperator()
	}
	
	List loadAllOperator(){
	    if(!operatorList){
			operatorList << loadDateOperator()
			operatorList << loadNumberOperator()
			operatorList << loadStringOperator()
			operatorList << loadCodeOperator()
			operatorList << loadBooleanOperator()
			operatorList << loadNoneOperator()
			//operatorList << loadEntityOperator()
			operatorList << loadYesNoOperator()
		}		
		operatorList
	}
	
	List loadDateOperator(){
		if(!dateOperatorList){
			dateOperatorList.add(getDataType(allDates,"All Dates"))
			dateOperatorList.add(getDataType(lastWeek,"Last Week"))
			dateOperatorList.add(getDataType(lastMonth,"Last Month"))
			dateOperatorList.add(getDataType(dateBetween,"Date Between"))
			dateOperatorList.add(getDataType(dateGreaterEqual,">= (Greater Than or Equal To)"))
			dateOperatorList.add(getDataType(dateLessEqual,"<= (Less Than or Equal To)"))
			dateOperatorList.add(getDataType(dateEqual,"= (Equal To)"))
			dateOperatorList.add(getDataType(datePastDays,"Past Days"))
			dateOperatorList.add(getDataType(datePastMonths,"Past Months"))
		}
		dateOperatorList
	}
	
	List loadNumberOperator(){
		if(!numOperatorList){
			numOperatorList.add(getDataType(numEqual,"= (Equal To)"))
			numOperatorList.add(getDataType(numGreaterEqual,">= (Greater Than or Equal To)"))
			numOperatorList.add(getDataType(numLessEqual,"<= (Less Than or Equal To)"))
			numOperatorList.add(getDataType(numBetween,"Between"))
			numOperatorList.add(getDataType(numNotEqual,"<> (Not Equal To)"))
		}
		numOperatorList
	}
	
	List loadStringOperator(){
		if(!strOperatorList){
			strOperatorList.add(getDataType(strStartsWith,"Starts With"))
			strOperatorList.add(getDataType(strContains,"Contains"))
			strOperatorList.add(getDataType(strMatchesExactly,"Matches Exactly"))
		}
		strOperatorList
	}
	
	List loadCodeOperator(){
		if(!codeOperatorList){
			codeOperatorList.add(getDataType(codeIdIsNumber,"Code_Id is a number"))
			codeOperatorList.add(getDataType(codeIdIsString,"Code_Id is a string"))
		}
		codeOperatorList
	}
	
	List loadBooleanOperator(){
		if(!booleanOperatorList){
			booleanOperatorList.add(getDataType(booleanNone,"N/A"))
			booleanOperatorList.add(getDataType(booleanTrue,"True"))
			booleanOperatorList.add(getDataType(booleanFalse,"False"))
		}
		booleanOperatorList
	}
	
	List loadNoneOperator(){
		if(!noneOperatorList)
			noneOperatorList.add(getDataType(none,"None"))
		noneOperatorList
	}
	
	List loadEntityOperator(){
		if(!entityOperatorList)
			entityOperatorList.add(getDataType(entityIdIsNumber,"Entity_Id is a number"))
		entityOperatorList
	}
	
	List loadYesNoOperator(){
		if(!yesNoOperatorList){
			yesNoOperatorList.add(getDataType(yesNoNone,"N/A"))
			yesNoOperatorList.add(getDataType(yesNoY,"Y - Yes"))
			yesNoOperatorList.add(getDataType(yesNoN,"N - No"))
		}
		yesNoOperatorList
	}
	
	Code getDataType(String id,String desc){
		Code operator = new Code()
		operator.setId(id)
		operator.setCodeDesc(desc)
		operator
	}	
}